package com.lxyk.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxyk.mapper.WorkerMapper;
import com.lxyk.pojo.Worker;
import com.lxyk.service.WorkerService;
import com.lxyk.utils.ConstantOssPropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

@Service
@Slf4j
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper,Worker>implements WorkerService {

    @Autowired
    private WorkerMapper workerMapper;

    @Override
    public int saveWorker(Worker worker) {
        return workerMapper.insert(worker);
    }

    @Override
    public Worker getWorkerById(Integer wid) {
        QueryWrapper<Worker> qw = new QueryWrapper<>();
        qw.eq("wid",wid);
        return workerMapper.selectOne(qw);
    }

    @Override
    public int updateWorker(Worker worker) {
        QueryWrapper<Worker> qw = new QueryWrapper<>();
        qw.eq("wid",worker.getWid());
        return workerMapper.update(worker,qw);
    }


    @Override
    public IPage<Worker> queryByWorker(Worker worker, Integer current, Integer size) {
        Page<Worker> page = new Page<>(current, size);
        QueryWrapper<Worker> qw = new QueryWrapper<>();
        if (worker.getWorkerName() != null && !worker.getWorkerName().isEmpty() ){
            qw.like("worker_name",worker.getWorkerName());
        }
        return workerMapper.selectPage(page,qw);
    }

    @Override
    public List<Worker> listWorker() {
        QueryWrapper<Worker> qw = new QueryWrapper<>();
        return workerMapper.selectList(qw);
    }

    @Override
    public String upload(MultipartFile file) {
        //地域节点
        String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
        String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
        String secret = ConstantOssPropertiesUtils.SECRECT;
        //bucket
        String bucketName = ConstantOssPropertiesUtils.BUCKET;

        try {
            //创建ossClient
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secret);
            //上传文件流
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();

            //相同文件会被覆盖
            //生成唯一id uuid 添加到文件名中
            String uuid = UUID.randomUUID().toString().replace("-","");
            fileName = uuid + fileName;

            //按照日期创建文件夹
            //2023.2.22/01.jpg
            String timeUrl = new DateTime().toString("yyyy/MM/dd");
            fileName = timeUrl+"/"+fileName;

            //调用方法实现上传
            ossClient.putObject(bucketName,fileName,inputStream);
            //关闭ossClient
            ossClient.shutdown();;
            //上传文件后文件路径
            String url = "https://" + bucketName+"."+endpoint+"/"+fileName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}